home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / FILECOMP.C < prev    next >
Text File  |  1993-06-26  |  7KB  |  278 lines

  1. /* : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
  2.     Program to compare (byte for byte)
  3.     two files and print differences
  4.  
  5.     H.Moran        10/27/79
  6.     slight mod    2/13/80
  7.    : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : */
  8.  
  9. /* : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
  10.     Macros for constant definitions
  11.    : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : */
  12.  
  13. #define EOFF -1        /* end of file marker returned by getc() */
  14. #define EOF 0x1A    /* ascii end of file mark */
  15. #define NOFILE -1    /* no such file indication given by fopen() */
  16. #define FALSE 0
  17. #define TRUE 1
  18.  
  19.  
  20. /*    -------------------------------------------------------
  21.  
  22.     Name:        main(argc,argv)
  23.     Result:        ---
  24.     Errors:        invocation syntax error or no such file
  25.     Globals:    ---
  26.     Macros:        TRUE,FALSE,NOFILE
  27.     Procedures:    fopen(),tolower(),bummer(),htoi(),fcompare()
  28.  
  29.     Action:        Byte by byte compare of 2 files and
  30.             print their differences on console
  31.  
  32.     ------------------------------------------------------- */
  33.  
  34.  
  35. main(argc,argv)
  36.     int argc;
  37.     char *argv[];
  38.     {
  39.     int fdin,fdout,ascii;
  40.     unsigned start_adrs,htoi();
  41.     char *ptr;
  42.     char mstbuf[134],chkbuf[134];
  43.  
  44.     ascii = FALSE;    /* assign the defaults */
  45.     start_adrs = 0;
  46.     if( argc < 3 || argc > 5 )
  47.       bummer();
  48.     else if( (fdin = fopen(argv[1],mstbuf)) == NOFILE )
  49.       printf("No such file %s\n",argv[1]);
  50.     else if( (fdout  = fopen(argv[2],chkbuf)) == NOFILE)
  51.       printf("No such file %s\n",argv[2]);
  52.     else  {
  53.       while( argc > 3 ) {
  54.         ptr = argv[--argc];
  55.         if( *ptr++ != '-' )
  56.           bummer();
  57.         switch ( tolower(*ptr++) ) {
  58.  
  59.           case 'a':    ascii = TRUE;
  60.             break;
  61.  
  62.           case 'b':    start_adrs = htoi(ptr);
  63.             break;
  64.  
  65.           default:    puts("Unrecognized option. Aborted\n\n");
  66.             bummer();
  67.           }    /* end switch */
  68.         }    /* end while */
  69.       fcompare(mstbuf,chkbuf,start_adrs,ascii);
  70.       }    /* end else */
  71.     exit();
  72.     }
  73.  
  74.  
  75. /*    -------------------------------------------------------
  76.  
  77.     Name:        fcompare(mfile,cfile,adrs,ascii)
  78.     Result:        ---
  79.     Errors:        ---
  80.     Globals:    ---
  81.     Macros:        EOFF,EOF
  82.     Procedures:    getc(),puts(),strcpy(),printf()
  83.  
  84.     Action:        compare 2 files and print their differences
  85.             on the console
  86.  
  87.     ------------------------------------------------------- */
  88.  
  89.  
  90.  
  91. fcompare(mfile,cfile,adrs,ascii)
  92.     char mfile[];        /* the input file buffer */
  93.     char cfile[];        /* the output file buffer */
  94.     unsigned adrs;        /* the address of begin of file */
  95.     int ascii;        /* flag of whether these are ascii files */
  96.     {
  97.     int mc,cc;        /* 1 char buffers */
  98.     char erflg;        /* flag that an error has occurred */
  99.     char *xl();        /* function to translate control chars */
  100.     char str1[6],str2[6];    /* temporaries for strings */
  101.     char xlate[10];    /* string used in ascii control char translation */
  102.  
  103.  
  104.     erflg = 0;
  105.     while( ! ( (mc = getc(mfile)) == EOFF || (ascii && mc == EOF)) ) {
  106.       if( (cc =getc(cfile)) == EOFF || ( ascii && cc == EOF )) {
  107.         puts("Checkfile shorter than Master file\n");
  108.         return;
  109.         }
  110.       else if( mc != cc ) {
  111.         if( ! erflg ) {
  112.           erflg = 1;
  113.           puts("\nRelative Master Check");
  114.           puts("\nAddress  File   File   Mismatch");
  115.           puts("\n-------  ----   ----   --------\n");
  116.           }    /* end if */
  117.         if( ascii )    {
  118.           strcpy(str1,xl(mc,xlate)); /* fudge because parameters are */
  119.           strcpy(str2,xl(cc,xlate)); /* evaluated before being passed */
  120.           printf("%4x     %-4s   %-4s   %8b\n",adrs,str1,str2,mc^cc);
  121.           }
  122.         else
  123.           printf("%4x     %2x     %2x     %8b\n",adrs,mc,cc,mc ^ cc);
  124.         }        /* end else if */
  125.       else
  126.         ;
  127.       adrs++;
  128.       }        /* end while */
  129.     if(! ( (cc = getc(cfile)) == EOFF || (ascii && cc == EOF) ) )
  130.       puts("Masterfile shorter than checkfile\n");
  131.     return;
  132.     }        /* end fcompare() */
  133.  
  134. /*    -------------------------------------------------------
  135.  
  136.     Name:        err_exit(msg)
  137.     Result:        ---
  138.     Errors:        ---
  139.     Globals:    ---
  140.     Macros:        ---
  141.     Procedures:    printf(),exit()
  142.  
  143.     Action:        Print a message then exit to CP/M 
  144.  
  145.     ------------------------------------------------------- */
  146.  
  147.  
  148. err_exit(msg)
  149.     char *msg;
  150.     {
  151.     exit(puts(msg));
  152.     }
  153.  
  154. /*    -------------------------------------------------------
  155.  
  156.     Name:        htoi(string)
  157.     Result:        unsigned integer value of ascii hex string
  158.     Errors:        ---
  159.     Globals:    ---
  160.     Macros:        ---
  161.     Procedures:    tolower(),isalpha(),isdigit()
  162.  
  163.     Action:        ---
  164.  
  165.     ------------------------------------------------------- */
  166.  
  167.  
  168. unsigned htoi(string)
  169.     char *string;
  170.     {
  171.     unsigned number;
  172.     char c;
  173.  
  174.     number = 0;
  175.     c = tolower(*string++);
  176.     while( isalpha(c) || isdigit(c) ) {
  177.       if( c > 'f' )
  178.         return number;
  179.       number *= 16;
  180.       if( isdigit(c) )
  181.         number += c -'0';
  182.       else
  183.         number += c - 'a' + 10;
  184.       c = tolower(*string++);
  185.       }
  186.     return number;
  187.     }
  188. /*    -------------------------------------------------------
  189.  
  190.     Name:        bummer()
  191.     Result:        ---
  192.     Errors:        ---
  193.     Globals:    ---
  194.     Macros:        ---
  195.     Procedures:    puts(),exit()
  196.  
  197.     Action:        Print the invocation syntax error message
  198.             and exit to CP/M
  199.  
  200.     ------------------------------------------------------- */
  201.  
  202.  
  203. bummer()
  204.     {
  205.     puts("Correct invocation form is:\n");
  206.     puts(" FILECOMP <master file> <check file> {-a -b<hex-num>}\n\n");
  207.     puts("Where optional arguments are:\n\n");
  208.     puts("-a          => these are ascii files (terminate on 1AH )\n");
  209.     puts("-b<hex-num> => begin of file is address <hex-num> ");
  210.     puts("default is 0\n");
  211.     exit();
  212.     }
  213.  
  214.  
  215. /*    -------------------------------------------------------
  216.  
  217.     Name:        xl(c)
  218.     Result:        pointer to  xlate[]
  219.     Errors:        ---
  220.     Globals:
  221.     Macros:        ---
  222.     Procedures:    strcpy()
  223.  
  224.     Action:        Translate the char argument c into a
  225.             4 char string in  xlate[]
  226.             If c is a printable ascii char its
  227.               translation is itself right blank padded
  228.             Else if c is a standard control char its
  229.               translation is a string identifying that
  230.               control char
  231.             Else its translation is "????"
  232.  
  233.     ------------------------------------------------------- */
  234.  
  235.  
  236. char *xl(c,xlate)
  237.     int c;
  238.     char *xlate;
  239.     {
  240.     if( c > 0x7f || c < 0 )
  241.       strcpy(xlate,"????");
  242.     else if( c == 0x7f )
  243.       strcpy(xlate,"del ");
  244.     else if( c > 0x1f ) {    /* then it is printable */
  245.       xlate[0] = c;
  246.       strcpy(xlate+1,"   ");
  247.       }
  248.     else
  249.       switch (c) {
  250.         case 0x7:    strcpy(xlate,"bel");
  251.             break;
  252.  
  253.         case 0x8:    strcpy(xlate,"bs");
  254.             break;
  255.  
  256.         case 0x9:    strcpy(xlate,"tab");
  257.             break;
  258.  
  259.         case 0xa:    strcpy(xlate,"lf");
  260.             break;
  261.  
  262.         case 0xc:    strcpy(xlate,"ff");
  263.             break;
  264.  
  265.         case 0xd:    strcpy(xlate,"cr");
  266.             break;
  267.  
  268.         case 0x1b:    strcpy(xlate,"esc");
  269.             break;
  270.  
  271.         default:    xlate[0] = '^';        /* show control chars as */
  272.             xlate[1] = c + 0x40;    /* ^ <char> e.g. ^A is */
  273.             xlate[2] = '\0';    /* control A */
  274.             break;
  275.         }
  276.     return xlate;
  277.     }
  278.